











import pandas as pd





df_home_page = pd.read_csv("./datas/ecommerce-website-funnel-analysis/home_page_table.csv")
df_search_page = pd.read_csv("./datas/ecommerce-website-funnel-analysis/search_page_table.csv")
df_payment_page = pd.read_csv("./datas/ecommerce-website-funnel-analysis/payment_page_table.csv")
df_payment_confirmation_page = pd.read_csv("./datas/ecommerce-website-funnel-analysis/payment_confirmation_table.csv")
df_user_table = pd.read_csv("./datas/ecommerce-website-funnel-analysis/user_table.csv")





df_home_page.head(3)


# 给page列做重命名
df_home_page.rename(columns={"page": "home_page"}, inplace=True)
df_home_page.head(3)





df_search_page.head(3)


# 给page列做重命名
df_search_page.rename(columns={"page": "search_page"}, inplace=True)
df_search_page.head(3)





df_payment_page.head(3)


# 给page列做重命名
df_payment_page.rename(columns={"page": "payment_page"}, inplace=True)
df_payment_page.head(3)





df_payment_confirmation_page.head(3)


# 给page列做重命名
df_payment_confirmation_page.rename(columns={"page": "confirmation_page"}, inplace=True)
df_payment_confirmation_page.head(3)





df_user_table.head(3)





df_user_table["device"].value_counts()





df_user_table["sex"].value_counts()





df_merge = df_user_table

for df_inter in [df_home_page, df_search_page, df_payment_page, df_payment_confirmation_page]:
    # 每次循环都会忘df_merge中添加新列
    df_merge = pd.merge(
        left=df_merge, 
        right=df_inter, 
        left_on="user_id", 
        right_on="user_id", 
        how="left"
    )


df_merge


#df_merge.columns = [
#    "user_id", "date", "device", "sex", 
#    "home_page", "search_page", "payment_page", "confirmation_page"]
#df_merge


df_merge.shape





datas = []
for column in ["home_page", "search_page", "payment_page", "confirmation_page"]:
    user_count = df_merge[column].dropna().size
    datas.append((column, user_count))
print(datas)


# 方便查看对比，进行归一化
max_count = datas[0][1]
datas_norm = [
              (x, round(y*100/max_count, 2)) 
              for x,y in datas
             ]
datas_norm





!pip install pyecharts


from pyecharts import options as opts
from pyecharts.charts import Funnel


funnel = Funnel()
funnel.add("用户比例", datas_norm)


funnel.render_notebook()



